package com.datang.pet.api.shoppingController;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-12 10:36
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.response.Result;
import com.datang.pet.common.utils.MD5;
import com.datang.pet.pojo.ShoppingCar;
import com.datang.pet.service.ShoppingCarService;
import com.datang.pet.vo.DeleteGoodsRequest;
import com.datang.pet.vo.GetShoppingCarList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ShoppingCarController {
    @Autowired
    private ShoppingCarService shoppingCarService;

    @PostMapping("/addGoods")
    public Result addGoods(@RequestBody ShoppingCar shoppingCar) throws Exception {
        try {
            shoppingCar.setUserId(MD5.decryptData(shoppingCar.getUserId()));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }

        return Result.ok(shoppingCarService.addGoods(shoppingCar));
    }

    @PostMapping("/deleteGoods")
    public Result deleteGoods(@RequestBody DeleteGoodsRequest deleteGoodsRequest) throws Exception {
        try {
            deleteGoodsRequest.setUserId(MD5.decryptData(deleteGoodsRequest.getUserId()));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }
        return Result.ok(shoppingCarService.deleteGoods(deleteGoodsRequest.getSpecIds(), deleteGoodsRequest.getUserId()));
    }

    @PostMapping("/updateGoodsQuantity")
    public Result updateGoodsQuantity(@RequestBody  ShoppingCar shoppingCar) throws Exception {
        try {
            shoppingCar.setUserId(MD5.decryptData(shoppingCar.getUserId()));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }

        return Result.ok(shoppingCarService.updateGoodsQuantity(shoppingCar));
    }

    @GetMapping("/getShoppingCarList")
    public Result getShoppingCarList(String userId) throws Exception {
        try {
            userId = MD5.decryptData(userId);
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }
        return Result.ok(shoppingCarService.getShoppingCarList(userId));
    }

}

